package band.wukong.practice.mina.quickstart.reverser;

import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import static band.wukong.practice.mina.quickstart.reverser.ReverseServer.PORT;

/**
 * Desc:
 * author: wukonggg@139.com
 */
public class ReverseClient {

    private final static Logger LOGGER = LoggerFactory.getLogger(ReverseProtocolHandler.class);

    public static void main(String[] args) throws Exception {
        NioSocketAcceptor acceptor = new NioSocketAcceptor();

        // Prepare the configuration
        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
        acceptor.getFilterChain().addLast("codec",
                new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

        // Bind
        acceptor.setHandler(new ReverseProtocolHandler());
        acceptor.bind(new InetSocketAddress(ReverseServer.HOSTNAME, PORT));

        LOGGER.info("Listening on port " + PORT);
    }

}
